package com.isunland.managesystem.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.ToastUtil;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

  /**
   * 微信appid
   */
  //public static final String WECHATPAY_APPID = "wxbe09e9d62ed64d81";
  public static final String WECHATPAY_APPID = "wx59fa8bfc52f2ab14";
  /** 国欣商户的key */
  //public static final String WECHATPAY_KEY = "827ccb0eea8a706c4c34a16891f84e7b";
  public static final String WECHATPAY_KEY = "vigafkgafkfglafsjfhsdfskjsajfsdj";
  /** 韦加商户key */
  //public static final String WECHATPAY_KEY = "ahgawlrugfhfwjfwarnfnwrvrjngvjvn";
  //private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
  public static final String ACTION_SHOW_NOTIFICATION =
      "com.isunLand.managesystem.weijia.ACTION_SHOW_NOTIFICATION";
  public static final String PERM_PRIVATE = "com.isunLand.managesystem.weijia.PERM_PRIVATE";
  public static final String EXTRA_ERRCODE = "com.isunLand.managesystem.weijia.EXTRA_ERRCODE";
  public static final int VALUE_SUCCESS = 0;
  private IWXAPI api;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.pay_result);

    api = WXAPIFactory.createWXAPI(this, WECHATPAY_APPID);
    api.handleIntent(getIntent(), this);
  }

  @Override protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    api.handleIntent(intent, this);
  }

  @Override public void onReq(BaseReq req) {
    LogUtil.d("weChatParams=", req);
  }

  //回调结果的处理
  @Override public void onResp(BaseResp resp) {
    ToastUtil.show("onPayFinish, errCode = " + resp.errCode);
    Intent i = new Intent(ACTION_SHOW_NOTIFICATION);
    i.putExtra(EXTRA_ERRCODE, resp.errCode);
    sendBroadcast(i, PERM_PRIVATE);
    finish();

    //if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
    //  if (resp.errCode == 0) {
    //
    //    ToastUtil.show("微信支付成功");
    //  } else {
    //
    //    ToastUtil.show("微信支付失败,请重试");
    //  }

    //sendOrderedBroadcast(i, PERM_PRIVATE, null, null, Activity.RESULT_OK, null, null);
    //}

  }
}